import 'package:flutter/widgets.dart';

import '../../res/colors.dart';
import '../widgets/date_slector/core/date_model.dart';
import '../widgets/date_slector/core/datetime_picker_theme.dart';
import '../widgets/date_slector/core/i18n_model.dart';
import '../widgets/date_slector/flutter_datetime_picker.dart';

class DateSelectorUtil {
  DateSelectorUtil._();

  static void showDateSelector(BuildContext context,
      {DateChangedCallback? onChanged,
      DateChangedCallback? onConfirm,
      DateCancelledCallback? onCancel}) {
    DatePicker.showDatePicker(context,
        showTitleActions: true,
        minTime: DateTime(1900, 1, 1),
        maxTime: DateTime.now(),
        theme: const DatePickerTheme(
          itemStyle: TextStyle(
              color: YouthColors.color_333333,
              fontSize: 22),
            cancelStyle: TextStyle(
                color: YouthColors.color_333333,
                fontSize: 16,
                fontWeight: FontWeight.bold),
            doneStyle: TextStyle(
                color: YouthColors.color_F65C15,
                fontSize: 16,
                fontWeight: FontWeight.bold),
            itemPadding: 0),
        onCancel: onCancel,
        onChanged: onConfirm,
        onConfirm: onConfirm,
        currentTime: DateTime.now(),
        locale: LocaleType.zh);

    // DatePicker.showPicker(context,
    //     showTitleActions: true,
    //     theme: const DatePickerTheme(
    //           cancelStyle: TextStyle(color: YouthColors.color_333333, fontSize: 16),
    //           doneStyle: TextStyle(color: YouthColors.color_F65C15, fontSize: 16),
    //         ),
    //     pickerModel: CustomPicker(),
    //     locale: LocaleType.zh);

    // DatePicker.showDateTimePicker(context,locale: LocaleType.zh);
    // DatePicker.showTimePicker(context);
    // DatePicker.showTime12hPicker(context);
  }
}

class CustomPicker extends CommonPickerModel {
  String digits(int value, int length) {
    return '$value'.padLeft(length, "0");
  }

  CustomPicker({DateTime? currentTime, LocaleType? locale})
      : super(locale: locale) {
    this.currentTime = currentTime ?? DateTime.now();
    setLeftIndex(this.currentTime.hour);
    setMiddleIndex(this.currentTime.minute);
    setRightIndex(this.currentTime.second);
  }

  @override
  String leftStringAtIndex(int index) {
    if (index >= 0 && index < 24) {
      return digits(index, 2);
    } else {
      return "";
    }
  }

  @override
  String middleStringAtIndex(int index) {
    if (index >= 0 && index < 60) {
      return digits(index, 2);
    } else {
      return "";
    }
  }

  @override
  String rightStringAtIndex(int index) {
    if (index >= 0 && index < 60) {
      return digits(index, 2);
    } else {
      return "";
    }
  }

  @override
  String leftDivider() {
    return "";
  }

  @override
  String rightDivider() {
    return "";
  }

  @override
  List<int> layoutProportions() {
    return [1, 1, 1];
  }

  @override
  DateTime finalTime() {
    return currentTime.isUtc
        ? DateTime.utc(currentTime.year, currentTime.month, currentTime.day,
            currentLeftIndex(), currentMiddleIndex(), currentRightIndex())
        : DateTime(currentTime.year, currentTime.month, currentTime.day,
            currentLeftIndex(), currentMiddleIndex(), currentRightIndex());
  }
}
